<?php
if(! defined("APP_START"))
	app_die("你有什麼陰謀 = =");

include("functions.php");

// 載入類別
$import = explode(",", $import);
foreach($import as $value)
	include(trim($value).".class.php");
unset($import, $value);

include("app.class.php");

// 建立基礎物件
$app = new app();
$cache = new cache();
$db = new db();
$session = new session();
$theme = new theme();
$upload = new upload();
$image = new image();

// 語言
$app->{"locale"} = null;
if(empty($_GET["lang"])){
	if(empty($_COOKIE["lang"])){
		$lang = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
		$lang = preg_replace("/([a-z]+)(\-[a-z]+)?/e", "'\\1'.strtr(strtoupper('\\2'), array('-'=>'_'))", $lang[0]);
		$app->locale = empty($app->lang[$lang])? DEFAULT_LANG: $lang;
	}else
		$app->locale = $_COOKIE["lang"];
}else{
	$app->locale = empty($app->lang[$_GET["lang"]])? DEFAULT_LANG: $_GET["lang"];
	setcookie("lang", $app->locale, time() + 2592000);
}
setlocale(LC_ALL, "{$app->locale}.utf8");
//bindtextdomain('default', APP_PATH.'core/language');
//textdomain('default');

// 載入Options
$res = $db->conn->prepare("SELECT * FROM `".$app->table["options"]."` WHERE `preload` = '1'");
$res->execute();
while($opt = $res->fetch(PDO::FETCH_ASSOC))
	$options[$opt["key"]] = $opt["value"];
$app->{"options"} =& $options;
unset($opt, $res);

// 路由
preg_match("/(?P<super>[\w]*)(?:-([\w]+))*/i", strip_tags(trim($_GET["page"])), $page);
switch(true){
	case empty($page["super"]):
	case $page["super"] == "home":
	case $page["super"] == "index":
		$request = "home";
		break;
	case $page["super"] == "myfd" && !empty($page[2]):
		$page["user"] = $page[2];
		$request = "myfd";
		break;
	case $page["super"] == "settings":
		$request = "settings";
		break;
	case $page["super"] == "search":
		$page["search"] = $page[2];
		$request = "search";
		break;
	case $page["super"] == "admin":
		$page["cate"] = $page[2];
		$page["setting"] = $page[3];
		$request = "search";
		break;
	default:
		$request = "404";
}
$theme->setOutputPage($request);

// 資料請求（$outputType 設置輸出格式）
include("{$request}.request.php");

// 資料加工
include("{$request}.process.php");

// 套用樣式
$theme->output();

// App end
$session->close();
?>